// 获取电话号码
var cookie = document.cookie,
    tel = cookie.slice(-15, cookie.length-4),
//        code = cookie.slice(-4, cookie.length);
    code = '9999';
var time = getEleCls('time'),
    anew = getEleCls('anew'),
    setPas = getEleCls('setPas'),
    delImg = getEleCls('delImg'),
    nextBtn = getEleCls('nextBtn'),
    hintBom = getEleCls('hintBom'),
    hint = getEleCls('hint'),
    auth = getEleCls('auth');
var countdown=10;
var timing = null;
hint.innerHTML = '<span>验证短信已发送至</span>'+'+86 '+tel;
//    输入框自定义属性值
var count1 = false;
var count2 = false;

//    验证
var verify = /^[0-9]{6}$/;
//    验证码输入
//   观察者模式
var Event = {
    event:{},
    handle:{},
    on:function (eventType, eventHandle) {
        this.handle[eventType]||(this.handle[eventType] = []);
        this.handle[eventType].push(eventHandle);
    },
    trigger:function(eventType){
        this.handle[eventType]&&this.handle[eventType].forEach(function (fn) {
            fn.call(undefined, event);
        })
    },
    addFlag:function () {
        this.event.flag = [];
    }
};

//  不满足验证码， 密码输入执行
Event.on('down', function () {
    nextBtn.style.cssText = 'background:#ccc;';
    nextBtn.onclick = null;
});
//验证码输入框
auth.oninput = function () {
    if((auth.value.length === 4)&&(auth.value === code)){
       count1 = true;
    }else{
       count1 = false;
    }
    ((count1&&count2)&&((auth.value.length === 4)&&(auth.value === code)))?Event.trigger('active'):Event.trigger('down')
};
//密码输入框
setPas.oninput = function () {
    if( setPas.value.length > 0) {
        delImg.style.cssText = 'display:block;';
        delImg.onclick = function () {
            setPas.value = '';
            delImg.style.cssText = 'display:none;';
            Event.trigger('down');
        };
    }else{
        delImg.style.cssText = 'display:none;';
    };

    if((setPas.value.length === 6)&&(verify.test(setPas.value))){
        count2 = true;
    }else{
        count2 = false;
    };
    ((count1&&count2)&&((setPas.value.length === 6)&&verify.test(setPas.value)))?Event.trigger('active'):Event.trigger('down')

};

//    定时器
function setTime(val, valAnew, timeNumber) {
    if (countdown === 0) {
        clearInterval(timing);
        val.setAttribute('id', 'none');
        valAnew.setAttribute('id', 'down');
        countdown = timeNumber || 0;
    } else {
        val.innerHTML= countdown + "s";
        countdown--;
    }
    timing = setTimeout(function() {
        setTime(val,valAnew)
    },1000)
}
setTime(time, anew);
//    淡入淡出
function show(showDom, time) {
    var temp = 17 / time;
    var showTime = 0;
    var hideTime = 1;
    var timer = setInterval(function () {
        if (showTime < 1) {
            showDom.style.opacity = showTime + '';
            if (showTime > 1) {
                clearInterval(timer);
                showDom.style.opacity = '1';
            }
            showTime += temp;
        } else {
            showDom.style.opacity = hideTime + '';
            if (hideTime < 0) {
                clearInterval(timer);
                showDom.style.opacity = '0';
            }
            hideTime -= temp;
        }
    }, 17);
}

//    重新发送验证码
anew.onclick = function () {
    count1 = false;
    count2 = false;
    // nextBtn.onclick = null;
    auth.value = '';
    setPas.value = '';
    setTime(time,anew,10);
    time.innerHTML = '10s';
    time.setAttribute('id', 'down');
    anew.setAttribute('id', 'none');
    delImg.style.cssText = 'display:none;';
    Event.trigger('down');
    ajax({
            method:'post',
            url:'http://localhost:8080/register',
            data:{
                telNumber:tel,
                step:'1'
            },
            dataType:'json',
            contentType:'json',
            success:function (data) {
                show(hintBom,2000);
                hintBom.innerHTML = cookie;
            },
            fail:function (error) {
                console.log(error);
            }
        });

};
//  满足验证码输入正确， 密码输入正确执行
Event.on('active', function () {
    nextBtn.style.cssText = 'background:#FA52AD;';
    //下一步， 将密码返回给后台
    if(count1&&count2){
        nextBtn.onclick = function () {
            ajax({
                method:'post',
                url:'http://localhost:8080/register',
                data:{
                    validCode:auth.value,
                    password:setPas.value,
                    step:'2'
                },
                dataType:'json',
                contentType:'json',
                success:function (data) {
                    //注册成功进行页面跳转
                    if(data.code === 1){
                        window.location.href = "./personal.html";
                    }
                },
                fail:function (error) {
                    console.log(error);
                }
            });
        };
    }
});